﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class ManageServiceStation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindServiceStation();
            }
        }

        private void BindServiceStation()
        {
            ServiceStationService service = new ServiceStationService();
            productVersionGridView.DataSource = service.GetALLServiceStations();
            productVersionGridView.DataBind();
        }

        protected void editLinkButton_Click(object sender, EventArgs e)
        {
            ServiceStationService service = new ServiceStationService();
            hfServiceStation.Value = ((LinkButton)sender).CommandArgument;
            ServiceStation editStation = service.GetServiceStation(Convert.ToInt32(hfServiceStation.Value));
            serviceStationNameTextBox.Text = editStation.Name;
            phoneTextBox.Text = editStation.Phone;
            faxTextBox.Text = editStation.Fax;
            emailTextBox.Text = editStation.EMail;
            cityTextBox.Text = editStation.City;
            forShortTextBox.Text = editStation.ForShort;
            addressTextBox.Text = editStation.Address;
            contactPersonTextBox.Text = editStation.ContactPerson;
            saveLinkButton.Text = "更新";
            serviceStationPopupExtender.Show();
        }

        protected void deleteLinkButton_Click(object sender, EventArgs e)
        {
            string serviceStationID = ((LinkButton)sender).CommandArgument;
            try
            {
                ServiceStationService service = new ServiceStationService();
                service.DeleteServiceStation(Convert.ToInt32(serviceStationID));
                BindServiceStation();
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('您不能删除这个平台');", true);
            }

        }

        protected void productVersionGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < productVersionGridView.PageCount)
            {
                productVersionGridView.PageIndex = e.NewPageIndex;
            }
            
            BindServiceStation();
        }

        protected void productVersionGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void addButton_Click(object sender, EventArgs e)
        {
            serviceStationPopupExtender.Show();
            saveLinkButton.Text = "添加";
            serviceStationNameTextBox.Text = "";
            phoneTextBox.Text = "";
            faxTextBox.Text = "";
            emailTextBox.Text = "";
            cityTextBox.Text = "";
            forShortTextBox.Text = "";
            addressTextBox.Text = "";
            contactPersonTextBox.Text = "";
        }

        protected void saveLinkButton_Click(object sender, EventArgs e)
        {
            ServiceStationService service = new ServiceStationService();
            if (saveLinkButton.Text.Equals("添加"))
            {
                ServiceStation station = new ServiceStation();
                station.Name = serviceStationNameTextBox.Text;
                station.Phone = phoneTextBox.Text;
                station.Fax = faxTextBox.Text;
                station.ForShort = forShortTextBox.Text;
                station.ContactPerson = contactPersonTextBox.Text;
                station.EMail = emailTextBox.Text;
                station.City = cityTextBox.Text;
                station.Address = addressTextBox.Text;

                service.AddServiceStation(station);
            }
            else
            {
                ServiceStation editStation = service.GetServiceStation(Convert.ToInt32(hfServiceStation.Value));
                editStation.Name = serviceStationNameTextBox.Text;
                editStation.Phone = phoneTextBox.Text;
                editStation.Fax = faxTextBox.Text;
                editStation.ForShort = forShortTextBox.Text;
                editStation.ContactPerson = contactPersonTextBox.Text;
                editStation.EMail = emailTextBox.Text;
                editStation.City = cityTextBox.Text;
                editStation.Address = addressTextBox.Text;

                service.UpdateServiceStation(editStation);
            }

            BindServiceStation();
        }
    }
}
